home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cwscr.exe / MYSCR.CPP < prev    next >
Text File  |  1991-07-28  |  6KB  |  234 lines

  1. /*
  2.     Text Screen module class specification.
  3.  
  4.     This is the source file that contains the actual source to the
  5.     routines used to output text based information to the screen.
  6.  
  7.     Library : mycpp.lib
  8. */
  9.  
  10. static char MyScr_Id[] = "myscr.cpp     1.00 05/22/91";
  11. /*
  12.     Version notes :
  13.         1.00 - Original creation / release. ( 2-14-91 CW )
  14.         1.10 - Added scroll routines. Also changed all of the char * modifiers
  15.                 to const char * where possible. ( 5-22-91 CW )
  16. */
  17.  
  18.  
  19. #include <dos.h>
  20. #include "myscr.h"
  21.  
  22.  
  23. #define XMAX    80
  24. #define YMAX    24
  25.  
  26. #define SCR_POS( scr, x, y )    ((scr_type far *)(scr) + ((x) + ((y) * 80)))
  27.  
  28. /* ------------------------------------------------------------------ */
  29. void
  30. myScreen::init(){
  31.     union REGS regs;
  32.  
  33.     regs.h.ah = 15;
  34.     int86( 0x10, ®s, ®s );
  35.     int color = (( regs.h.al != 7 ) && ( regs.h.al != 96 ));
  36.     screen = (scr_type far *)( color ? 0xb8000000L : 0xb0000000L );
  37.  
  38.     cursor_show();
  39. }
  40.  
  41. /* .................................................................. */
  42. void
  43. myScreen::print( int x, int y, int attr, const char *str ){
  44.     tscr = SCR_POS( screen, x, y );
  45.     while( *str && ( ++x <= XMAX )){
  46.         if( attr != -1 )
  47.             tscr->attrib = attr;
  48.         (tscr++)->ch = *str++;
  49.     }
  50. }
  51.  
  52. /* .................................................................. */
  53. void
  54. myScreen::print( int x, int y, int attr, const char chr ){
  55.     tscr = SCR_POS( screen, x, y );
  56.     if( attr != -1 )
  57.         tscr->attrib = attr;
  58.     (tscr++)->ch = chr;
  59. }
  60.  
  61. /* .................................................................. */
  62. void
  63. myScreen::clear( int x, int y, int w, int h, int attr ){
  64.     while(( h-- > 0 ) && ( y < YMAX )){
  65.         tscr = SCR_POS( screen, x, y );
  66.         for( int i = 0; (i < w) && ((i + x) < XMAX); i++ ){
  67.             if( attr != -1 )
  68.                 tscr->attrib = attr;
  69.             (tscr++)->ch = ' ';
  70.         }
  71.         y++;
  72.     }
  73. }
  74.  
  75. /* .................................................................. */
  76. void
  77. myScreen::change( int x, int y, int w, int h, int attr ){
  78.     if( attr != -1 ){
  79.         while(( h-- > 0 ) && ( y < YMAX )){
  80.             tscr = SCR_POS( screen, x, y );
  81.             for( int i = 0; (i < w) && ((i + x) < XMAX); i++ ){
  82.                 (tscr++)->attrib = attr;
  83.             }
  84.             y++;
  85.         }
  86.     }
  87. }
  88.  
  89. /* .................................................................. */
  90. void
  91. myScreen::scroll( ScrollDir dir, int x, int y, int w, int h, const char *str ){
  92.     scr_type    far *tscr2;
  93.  
  94.     if( dir == Scroll_Up ){
  95.         for( int rows = 0; rows < ( h-1 ); rows++ ){
  96.             tscr = SCR_POS( screen, x, y + rows );
  97.             tscr2 = SCR_POS( screen, x, y + rows + 1 );
  98.             for( int cols = 0; cols < w; cols++ ){
  99.                 *tscr++ = *tscr2++;
  100.             }
  101.         }
  102.         clear( x, y + h - 1, w, 1 );
  103.         if( strlen( str ) > 0 )
  104.             print( x, y + h - 1, str );
  105.     } else if( dir == Scroll_Down ){
  106.         for( int rows = ( h-1 ); rows >= 0; rows-- ){
  107.             tscr = SCR_POS( screen, x, y + rows );
  108.             tscr2 = SCR_POS( screen, x, y + rows - 1 );
  109.             for( int cols = 0; cols < w; cols++ ){
  110.                 *tscr++ = *tscr2++;
  111.             }
  112.         }
  113.         clear( x, y, w, 1 );
  114.         if( strlen( str ) > 0 )
  115.             print( x, y, str );
  116.     }
  117. }
  118.  
  119. /* .................................................................. */
  120. const save_type *
  121. myScreen::save( int x, int y, int w, int h ){
  122.     int save_size = w * h;
  123.     save_type *data;
  124.  
  125.     data = new save_type;
  126.     data->x = x;
  127.     data->y = y;
  128.     data->w = w;
  129.     data->h = h;
  130.     data->scr_ptr = new scr_type[ save_size ];
  131.     scr_type *tptr = data->scr_ptr;
  132.  
  133.     while(( h-- > 0 ) && ( y < YMAX )){
  134.         tscr = SCR_POS( screen, x, y );
  135.         for( int i = 0; (i < w) && ((i + x) < XMAX); i++ ){
  136.             *tptr++ = *tscr++;
  137.         }
  138.         y++;
  139.     }
  140.  
  141.     return( data );
  142. }
  143.  
  144. /* .................................................................. */
  145. void
  146. myScreen::restore( const save_type *data ){
  147.     int x = data->x;
  148.     int y = data->y;
  149.     int w = data->w;
  150.     int h = data->h;
  151.     scr_type *tptr = data->scr_ptr;
  152.  
  153.     while(( h-- > 0 ) && ( y < YMAX )){
  154.         tscr = SCR_POS( screen, x, y );
  155.         for( int i = 0; (i < w) && ((i + x) < XMAX); i++ ){
  156.             *tscr++ = *tptr++;
  157.         }
  158.         y++;
  159.     }
  160. }
  161.  
  162. /* .................................................................. */
  163. /* .................................................................. */
  164. void
  165. myScreen::cursor_move( int x, int y ){
  166.     union REGS regs;
  167.  
  168.     regs.h.ah = 2;
  169.     regs.h.bh = 0;
  170.     regs.h.dl = x;
  171.     regs.h.dh = y;
  172.     int86( 0x10, ®s, ®s );
  173. }
  174.  
  175. /* .................................................................. */
  176. void
  177. myScreen::cursor_show(){
  178.     union REGS regs;
  179.  
  180.     regs.h.ah = 1;
  181.     regs.h.ch = 7;  // starting scan line.
  182.     regs.h.cl = 8;  // ending scan line.
  183.     int86( 0x10, ®s, ®s );
  184.  
  185.     cursor_flag = TRUE;
  186. }
  187.  
  188. /* .................................................................. */
  189. void
  190. myScreen::cursor_hide(){
  191.     union REGS regs;
  192.  
  193.     regs.h.ah = 1;
  194.     regs.x.cx = 0x2000;
  195.     int86( 0x10, ®s, ®s );
  196.  
  197.     cursor_flag = FALSE;
  198. }
  199.  
  200. /* ================================================================== */
  201.  
  202.  
  203. #ifdef debug_screen
  204.  
  205.  
  206. #include <conio.h>      // for getch() function call.
  207.  
  208. main(){
  209.     myScreen    theScreen;
  210.  
  211.     theScreen.clear();
  212.     theScreen.clear( 1, 5, 15, 5, SCR_Fi_White | SCR_B_Green );
  213.  
  214.     theScreen.print( 1, 5, "this is a test. this is a test. line 1" );
  215.     theScreen.print( 1, 6, "this is a test. this is a test. line 2" );
  216.     theScreen.print( 1, 7, "this is a test. this is a test. line 3" );
  217.     theScreen.print( 1, 8, "this is a test. this is a test. line 4" );
  218.     theScreen.print( 1, 9, "this is a test. this is a test. line 5" );
  219.  
  220.     if( !getch() ) getch();
  221.     theScreen.scroll( Scroll_Up, 5, 3, 10, 5 );
  222.     if( !getch() ) getch();
  223.     theScreen.scroll( Scroll_Down, 5, 3, 10, 5 );
  224.  
  225.     if( !getch() ) getch();
  226.     theScreen.scroll( Scroll_Up, 32, 5, 10, 5, " the line last." );
  227.  
  228.     if( !getch() ) getch();
  229.     return 0;
  230. }
  231.  
  232. #endif
  233.  
  234.